package com.pipayshop.service.impl;

import com.pipayshop.domain.StoreCommodityHistory;
import com.pipayshop.mapper.PhysicalStoreCommodityHistoryMapper;
import com.pipayshop.service.PhysicalStoreCommodityHistoryService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pipayshop.utils.ValidationUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 * 实体店用户商品历史记录表 服务实现类
 * </p>
 *
 * @author zxb
 * @since 2023-07-29
 */
@Service
public class PhysicalStoreCommodityHistoryServiceImpl extends ServiceImpl<PhysicalStoreCommodityHistoryMapper, StoreCommodityHistory> implements PhysicalStoreCommodityHistoryService {

    @Resource
    private PhysicalStoreCommodityHistoryMapper shopCommodityHistoryMapper;

    /**
     * 删除用户浏览商品的历史记录
     *
     * @param userId
     * @param commodityId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteHistory(String userId, String commodityId) {
        if (commodityId != null) {
            return shopCommodityHistoryMapper.delete(new LambdaQueryWrapper<StoreCommodityHistory>()
                    .eq(StoreCommodityHistory::getCommodityId, commodityId)
                    .eq(StoreCommodityHistory::getUserId, userId)) > 0;
        }
        if (userId != null) {
            return shopCommodityHistoryMapper.delete(new LambdaQueryWrapper<StoreCommodityHistory>()
                    .eq(StoreCommodityHistory::getUserId, userId)) > 0;
        }
        return false;
    }

    /**
     * 添加用户浏览商品的历史记录
     *
     * @param shopCommodityHistory
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addHistory(StoreCommodityHistory shopCommodityHistory) {
        // 校验传入对象是否为空
        ValidationUtil.validateNotNull(shopCommodityHistory, "history_record_not_empty");
        ValidationUtil.validateString(shopCommodityHistory.getCommodityId(), "commodity_id_required");
        ValidationUtil.validateString(shopCommodityHistory.getUserId(), "user_id_required");
        // 查询是否已有该商品和用户的历史记录
        StoreCommodityHistory histories = shopCommodityHistoryMapper.selectOne(new LambdaQueryWrapper<StoreCommodityHistory>()
                .eq(StoreCommodityHistory::getCommodityId, shopCommodityHistory.getCommodityId())
                .eq(StoreCommodityHistory::getUserId, shopCommodityHistory.getUserId()));
        int result;
        if (histories == null) {
            // 插入新记录
            result = shopCommodityHistoryMapper.insert(shopCommodityHistory);
        } else {
            // 更新已有记录的查看时间
            result = shopCommodityHistoryMapper.update(null, new LambdaUpdateWrapper<StoreCommodityHistory>()
                    .eq(StoreCommodityHistory::getCommodityId, shopCommodityHistory.getCommodityId())
                    .eq(StoreCommodityHistory::getUserId, shopCommodityHistory.getUserId())
                    .set(StoreCommodityHistory::getCreateTime, new Date())); // 设置当前时间
        }

        // 返回操作结果
        return result > 0;
    }


}
